//
//  GXTableNumberViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/30.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXTableNumberViewController.h"
#import "GXAddNewTableController.h"
#import "GXTableDetailController.h"
#import "GXTableNumberModel.h"
#import "TableNumberManager.h"
@interface GXTableNumberViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * mytableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic, strong) NSMutableArray *deleteArr;//删除数据的数组
@property (nonatomic,assign) NSInteger index;
@property (nonatomic,copy) NSString * storeId;
@end
static NSString *const tableCell=@"tableNumberCell";
@implementation GXTableNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"桌位";
    _index = -1;
    [self creatAllView];
    [self requestTableListData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveAddTableNotice) name:kNotificationAddTable object:nil];
}
- (void)receiveAddTableNotice{
    MyLog(@"receiveAddTableNotice");
     [self  requestTableListData];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)creatAllView
{
    self.view.backgroundColor = kGlobalbgColor;
    [self.view addSubview:self.mytableView];
    [self setupTableFooterView];
    [self setUpNavigationRrightBarButtonItem];
}
- (NSMutableArray *)deleteArr{
    if (_deleteArr == nil) {
        _deleteArr = [NSMutableArray array];
    }
    return _deleteArr;
}
#pragma 请求所有桌次数据
- (void)requestTableListData
{
    self.storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [TableNumberManager selectTableListWithStoreID:self.storeId success:^(id responseObject) {
        self.dataArray =[NSMutableArray arrayWithArray:responseObject];
         [self.mytableView reloadData];
    } fail:^(id responseObject) {
    }];
}


#pragma 创建UITableView
- (UITableView *)mytableView
{
    if (_mytableView == nil) {
        _mytableView=[[UITableView alloc] initWithFrame:CGRectMake(5,0,kUIScreenWidth-10,kUIScreenHeight-Height_NavBar) style:UITableViewStyleGrouped];
        _mytableView.delegate=self;
        _mytableView.dataSource=self;
        _mytableView.backgroundColor = kGlobalbgColor;
        _mytableView.editing =NO;
        _mytableView.sectionHeaderHeight = 15;
        _mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    }
    return _mytableView;
}
#pragma 创建tableFooterView
- (void)setupTableFooterView
{
    UIButton * footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    footBtn.frame = CGRectMake(0, 0, kUIScreenWidth, 45);
    footBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [footBtn setButtonTitle:@"编辑" selectTitle:@"删除" BackgroundColor:kGlobalButtonColor];
    [footBtn setCornerWithRadius:3];
    [footBtn addTarget:self action:@selector(clickMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.mytableView.tableFooterView = footBtn;
}
- (void)clickMoreBtn:(UIButton *)btn
{
    self.mytableView.allowsMultipleSelectionDuringEditing = YES;
    self.mytableView.editing = !self.mytableView.editing;
    btn.selected=!btn.selected;
    if (btn.selected) {
        if (self.mytableView.editing) {
            _index= -1;
        }
    }else{
        if (self.deleteArr.count) {
          [self requestDeleteTableData];
        }
    }
}
- (void)requestDeleteTableData
{
      NSString * deleteStr = nil;
    if (self.deleteArr.count) {
        GXTableNumberModel *model = self.deleteArr.lastObject;
        deleteStr = model.deId;
    }
    [TableNumberManager deleteTableNumberWithDeId:deleteStr success:^(id responseObject) {
        if (responseObject) {
            [self.dataArray removeObjectsInArray:self.deleteArr];
            self.mytableView.editing = NO;
            [self.mytableView reloadData];
            [self.deleteArr removeAllObjects];
        }
    } fail:^(id responseObject) {
        
    }];
}
#pragma 创建rightBarButtonItem
- (void)setUpNavigationRrightBarButtonItem
{
    UIButton * rightButton = [UIButton setButtonTitleNormal:@"新增" buttonTitlSelect:@"" buttonTintColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:13]];
    [rightButton addTarget:self action:@selector(clickRightItemButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}
- (void)clickRightItemButton:(UIButton *)rightBtn
{
    if (self.mytableView.editing) {
        [SVProgressHUD showImage:nil status:@"编辑中"];
        return;
    }
    GXAddNewTableController * tableVC = [[GXAddNewTableController alloc]init];
    [self.navigationController pushViewController: tableVC animated:YES];
}
#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:tableCell];
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tableCell];
        GXTableNumberModel * model = self.dataArray[indexPath.row];
        cell.textLabel.text = model.tabName;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@人",model.peopleNum];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 3;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.isEditing) {
        [self.deleteArr removeAllObjects];
        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0]; UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex]; lastCell.accessoryType = UITableViewCellAccessoryNone;
        lastCell.selected = YES;
        _index = indexPath.row;
        [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
        [self.mytableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:lastIndex afterDelay:.0];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        GXTableNumberModel * editModel =self.dataArray[indexPath.row];
        GXTableDetailController * detailVC = [[GXTableDetailController alloc]init];
        if (editModel) {
            detailVC.model = editModel;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
        WeakSelf(weakSelf);
        detailVC.saveBlock = ^(GXTableNumberModel * _Nonnull model) {
            [weakSelf.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        };
    }
}

//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
    lastCell.selected = NO;
    _index  = -1;
    [self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end
